<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Membership
 * @version 	1.0
 * @author 		Rukan <rukanhitokiri@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
class ZadminTab extends AdminTab
{	
	/**
	 * add element for form
	 *
	 * @param string $type type element html render
	 * @param array	$fields
     * @return string html
	 */
	public function addElement($type, $fields = array(),$lang=false)
	{	
		if ($type == 'text')
			return $this->renderTextInput($fields,$lang);
		elseif ($type == 'color')
			return $this->renderColorPicker($fields,$lang);
		elseif ($type == 'select')
			return $this->renderSelect($fields,$lang);
		elseif ($type == 'yesno')
			return $this->renderYesNoOption($fields,$lang);
		elseif ($type == 'area')	
			return $this->renderArea ($fields,$lang);
	}
	
	/**
	 * display Color Picker
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderColorPicker($fields = array())
	{

	}
	
	/**
	 * display TextInput
	 *
	 * @param array	$fields
     * @return string html
	 */
	public function renderTextInput($fields = array(),$lang=false)
	{	
		$obj = $this->loadObject(true);
		if (!$lang) {
			return '	
			<label>'.$this->l($fields['label']).'</label>
			<div class="margin-form">
				<input name="'.$fields['name'].'" '.$fields['more'].' id="'.$fields['name'].'" type="text" class="text" value="'.$fields['value'].'"/>
				<p class="clear">'.$this->l($fields['note']).'</p>
			</div>
			<div class="clear"></div>';
		}
		else {
			echo '<label>'.$this->l($fields['label']).':</label>
				<div class="margin-form">';
			foreach ($this->_languages as $language)
			echo '
					<div id="'.$fields['name'].'_'.$language['id_lang'].'" style="float: left;'.($language['id_lang'] != $this->_defaultFormLanguage ? 'display:none;' : '').'">
						<input style="width:'.$fields['width'].'px" id="'.$fields['name'].'_'.$language['id_lang'].'"   '.$fields['more'].'  name="'.$fields['name'].'_'.$language['id_lang'].'"value="'.htmlentities(stripslashes($this->getFieldValue($obj, $fields['name'], $language['id_lang'])), ENT_COMPAT, 'UTF-8').'"/><sup> *</sup>
						<p class="clear">'.$this->l($fields['note']).'</p>
					</div>';
			$this->displayFlags($this->_languages, $this->_defaultFormLanguage, $fields['langtags'], $fields['name']);
			
			echo '
			<div class="clear"> </div></div>';
		}
	}
	
		
	/**
	 * display Select option
	 * 
	 * @param array	$fields
     * @return string html
	 */
	public function renderSelect($fields = array())
	{
		echo '<label>'.$this->l($fields['label']).':</label>
				<div class="margin-form">';
		echo '<select style="width: '.$fields['width'].'px;" name="'.$fields['name'].'">';
		foreach ($fields['value'] as $value)
			echo '<option '.($value==$fields['check'] ? 'selected="selected" ' : '').' value="'.$value.'" >'.$value.'</option>';
		echo '</select><p class="clear">'.$this->l($fields['note']).'</p>';
		
		echo '
			<div class="clear"> </div></div>';
	}
	
	/**
	 * display YesNo option radio option
	 * 
	 * @param array	$fields
     * @return string html
	 */
	public function renderYesNoOption($fields = array())
	{
		if (!($obj = $this->loadObject(true)))
			return;
		return '	
		<label>'.$this->l($fields['label']).'</label>
		<div class="margin-form">
			<input type="radio" name="'.$fields['name'].'" id="'.$fields['name'].'_on" value="1" '.($this->getFieldValue($obj, $fields['name']) ? 'checked="checked" ' : '').'/>
			<label class="t" for="'.$fields['name'].'_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
			<input type="radio" name="'.$fields['name'].'" id="'.$fields['name'].'_off" value="0" '.(!$this->getFieldValue($obj, $fields['name']) ? 'checked="checked" ' : '').'/>
			<label class="t" for="'.$fields['name'].'_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
		</div>
		<div class="clear"></div>';
	}
	
	public function renderArea ($fields = array (),$lang = false) {
		
		if ($lang) {
			$obj = $this->loadObject(true);
			echo '
			<label>'.$this->l($fields['label']).'</label>
				<div class="margin-form">';
			foreach ($this->_languages as $language)
			echo '
					<div id="'.$fields['name'].'_'.$language['id_lang'].'" style="float: left;'.($language['id_lang'] != $this->_defaultFormLanguage ? 'display:none;' : '').'">
						<textarea name="'.$fields['name'].'_'.$language['id_lang'].'" rows="10" cols="100">'.htmlentities($this->getFieldValue($obj, $fields['name'], (int)($language['id_lang'])), ENT_COMPAT, 'UTF-8').'</textarea>
					</div>';
			$this->displayFlags($this->_languages, $this->_defaultFormLanguage, $fields['langtags'], $fields['name']);
			echo '	<div class="clear"></div>
					</div>';
		}
	}
}